package com.trip_memories;

import java.util.UUID;

import signedUser.Signed;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import blogic.AccesBussineLogic;
import blogic.BusinessLogic;
import dbmodels.User;
import encryption.MD5Encryption;

public class SignIN extends Activity {

	private static final int DIALOG_ALERT = 1;
	private static final int DIALOG_ALERT1 = 2;
	private static final int DIALOG_ALERT2 = 3;
	private static final int DIALOG_ALERT3 = 4;
	BusinessLogic bL;
	User user;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sign_in);
		bL = AccesBussineLogic.getBussinesLogic(this);
		user = new User();
	}

	@SuppressWarnings("deprecation")
	public void onClick(View view) {
		String name = ((EditText) findViewById(R.id.enter_name)).getText()
				.toString();
		String pass = ((EditText) findViewById(R.id.enter_pass)).getText()
				.toString();
		Log.d("date", "name " + name);
		Log.d("date", "pass " + pass);

		user.setId(UUID.randomUUID());
		user.setName(name);
		user.setPass(pass);
		Log.d("date", user.isValid() + "");
		if (user.isValid()) {
			user.setPass(MD5Encryption.encrypt(user.getPass()));
			if (!bL.existUserName(user.getName())) {
				showDialog(DIALOG_ALERT);
			} else {
				UUID userId = bL.existUser(user.getName(), user.getPass());
				if (userId == null) {
					showDialog(DIALOG_ALERT2);
				} else {
					user.setId(userId);
					showDialog(DIALOG_ALERT1);
				}

			}
		} else {
			showDialog(DIALOG_ALERT3);
		}

	}

	@Override
	@Deprecated
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_ALERT: {
			Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(R.string.user_not_exist);
			builder.setCancelable(true);
			builder.setNegativeButton(android.R.string.cancel,
					new CancelOnClickListener());
			builder.setPositiveButton(android.R.string.ok,
					new OkOnClickListener());
			AlertDialog dialog = builder.create();
			dialog.show();
		}
			break;
		case DIALOG_ALERT1: {
			Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(R.string.user_exist);
			builder.setPositiveButton(android.R.string.ok,
					new SignOnClickListener());
			AlertDialog dialog = builder.create();
			dialog.show();
		}
			break;
		case DIALOG_ALERT2: {
			Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(R.string.inc_pass);
			builder.setNegativeButton(android.R.string.cancel,
					new IncPassOnClickListener());
			AlertDialog dialog = builder.create();
			dialog.show();
		}
			break;
		case DIALOG_ALERT3: {
			Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(R.string.form_empty);
			builder.setNegativeButton(android.R.string.cancel,
					new FormEmptyOnClickListener());
			AlertDialog dialog = builder.create();
			dialog.show();
		}
			break;
		}
		return super.onCreateDialog(id);
	}

	// Add user
	private final class OkOnClickListener implements
			DialogInterface.OnClickListener {
		public void onClick(DialogInterface dialog, int which) {
			bL.AddUser(user);
			Signed.setId(user.getId());
			Log.d("date", user.getName() + " is signed");
			finish();
		}
	}

	// Cancel add user
	private final class CancelOnClickListener implements
			DialogInterface.OnClickListener {
		public void onClick(DialogInterface dialog, int which) {
		}
	}

	// Sign the user
	private final class SignOnClickListener implements
			DialogInterface.OnClickListener {
		public void onClick(DialogInterface dialog, int which) {
			Signed.setId(user.getId());
			finish();
		}
	}

	// Inccorect password
	private final class IncPassOnClickListener implements
			DialogInterface.OnClickListener {
		public void onClick(DialogInterface dialog, int which) {
		}
	}

	// Form Empty
	private final class FormEmptyOnClickListener implements
			DialogInterface.OnClickListener {
		public void onClick(DialogInterface dialog, int which) {
		}
	}
}
